package com.abyss.transformation.datastream;


import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * 演示流计算中的connect算子
 * connect算子可以将两个流连接在一起，和union不同的是，connect连接后，两个流还能够还原回去
 */
public class ConnectDemo {
    public static void main(String[] args) throws Exception {
        // 1. Env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2. 2个Source对象
        DataStreamSource<String> source1 = env.fromElements("haha", "hehe", "heihei");
        DataStreamSource<String> source2 = env.fromElements("lala", "papa", "gogo");

        // 3. connect
        ConnectedStreams<String, String> connect = source1.connect(source2);

        SingleOutputStreamOperator<Tuple2<String, Integer>> mapConnect = connect.map(new CoMapFunction<String, String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map1(String value) throws Exception {
                return Tuple2.of(value, 1);
            }

            @Override
            public Tuple2<String, Integer> map2(String value) throws Exception {
                return Tuple2.of(value, 1);
            }
        });

        mapConnect.print();

        env.execute();
    }
}